package main;

import game.controller.GameController;
import game.environments.GameEnvironment;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;
import listener.KeyboardListener;
import sprites.AllySprite;
import sprites.EnemySprite;
import sprites.ResponsiveSprite;
import view.ViewController;


/**
 * This is sample code showing how a game developer would use the framework.
 * 
 * @author Lance
 * 
 */
public class Main {

    private static final int SPRITE_SPEED = 8;

    public static GameEnvironment initLevel () throws ClassNotFoundException {
        // initialize game environment
        GameEnvironment gameEnv = new GameEnvironment();
        // gameEnv.setBackground("../resources/whiteBackground.jpg");
        gameEnv.setWinningScore(100);
        // initialize characters

        ResponsiveSprite mainCraft = new ResponsiveSprite("../resources/craft.png");
        mainCraft.configureInput(KeyEvent.VK_UP, KeyEvent.VK_DOWN, KeyEvent.VK_LEFT,
                                 KeyEvent.VK_RIGHT);
        mainCraft.configureHP(10);
        mainCraft.configurePosition(new Point(200, 300));
        mainCraft.addCollidableSprite(new EnemySprite());
        mainCraft.addCollidableSprite(new AllySprite());

        AllySprite weapon = new AllySprite("../resources/missile.png");
        weapon.configureHP(100);
        weapon.configureMovement(SPRITE_SPEED, "north");
        weapon.addCollidableSprite(new EnemySprite());

        mainCraft.configureWeapon(weapon);
        mainCraft.configureWeapon(KeyEvent.VK_SPACE);

        gameEnv.add(mainCraft);

        return gameEnv;
    }

    public static GameEnvironment initL1 () throws ClassNotFoundException {

        // initialize game environment
        GameEnvironment gameEnvL1 = initLevel();
        // gameEnvL1.setBackground("../resources/whiteBackground.jpg");

        AllySprite goldCoin = new AllySprite("../resources/goldCoin.png");
        goldCoin.configureMovement(SPRITE_SPEED, "south");
        goldCoin.addCollidableSprite(new ResponsiveSprite());

        EnemySprite rock = new EnemySprite("../resources/rock.png");
        rock.configureMovement(SPRITE_SPEED, "south");

        ArrayList<Integer> rockPosXList = new ArrayList<Integer>();
        int rockPosY = 0;
        Random rand = new Random();

        for (int i = 0; i < 10; i++) {
            rockPosXList.add(100);
            rockPosXList.add(300);
            rockPosXList.add(500);
            if (rand.nextInt(2) == 1) {
                int coinPos = rand.nextInt(rockPosXList.size());
                for (int j = 1; j <= 5; j++) {
                    AllySprite goldCoinCopy = new AllySprite(goldCoin);
                    goldCoinCopy.configurePosition(new Point(rockPosXList.get(coinPos),
                                                             rockPosY - (100 + j * 50)));
                    gameEnvL1.add(goldCoinCopy);
                }
            }

            int numRocks = rand.nextInt(2);
            for (int j = 0; j <= numRocks; j++) {
                int index = rand.nextInt(rockPosXList.size());
                int rockPosX = rockPosXList.get(index);
                EnemySprite rockCopy = new EnemySprite(rock);
                rockCopy.configurePosition(new Point(rockPosX, rockPosY));
                gameEnvL1.add(rockCopy);
                rockPosXList.remove(index);
            }
            rockPosY -= 700;
            rockPosXList.clear();
        }

        return gameEnvL1;
    }

    public static GameEnvironment initL2 () throws ClassNotFoundException {

        GameEnvironment gameEnvL2 = initLevel();
        // gameEnvL2.setBackground("../resources/whiteBackground.jpg");

        EnemySprite rock = new EnemySprite("../resources/rock.png");
        rock.configureMovement(SPRITE_SPEED, "south");

        EnemySprite alien = new EnemySprite("../resources/alien.png");
        alien.configureMovement(SPRITE_SPEED, "south");
        alien.addCollidableSprite(new ResponsiveSprite());
        alien.addCollidableSprite(new AllySprite());

        ArrayList<Integer> rockPosXList = new ArrayList<Integer>();
        int rockPosY = 0;
        Random rand = new Random();

        for (int i = 0; i < 10; i++) {
            rockPosXList.add(100);
            rockPosXList.add(300);
            rockPosXList.add(500);
            int numRocks = rand.nextInt(2);
            for (int j = 0; j <= numRocks; j++) {
                int index = rand.nextInt(rockPosXList.size());
                int rockPosX = rockPosXList.get(index);
                EnemySprite rockCopy = new EnemySprite(rock);
                rockCopy.configurePosition(new Point(rockPosX, rockPosY));
                gameEnvL2.add(rockCopy);
                rockPosXList.remove(index);
            }

            int index = rand.nextInt(rockPosXList.size());
            int alienPosX = rockPosXList.get(index);
            EnemySprite alienCopy = new EnemySprite(alien);
            alienCopy.configurePosition(new Point(alienPosX, rockPosY));

            gameEnvL2.add(alienCopy);

            rockPosY -= 700;
            rockPosXList.clear();
        }

        return gameEnvL2;
    }

    public static GameEnvironment initLevel2 () throws ClassNotFoundException {
        // initialize game environment
        GameEnvironment gameEnv = new GameEnvironment();
        // gameEnv.setBackground("../resources/whiteBackground.jpg");
        gameEnv.setWinningScore(100);
        // initialize characters

        ResponsiveSprite smiley = new ResponsiveSprite("../resources/smiley.png");
        smiley.configureInput(KeyEvent.VK_I, KeyEvent.VK_K, KeyEvent.VK_J, KeyEvent.VK_L);
        smiley.configureHP(20);
        smiley.configurePosition(new Point(100, 200));
        smiley.addCollidableSprite(new EnemySprite());
        smiley.addCollidableSprite(new AllySprite());

        AllySprite weapon = new AllySprite("../resources/water.png");
        weapon.configureHP(100);
        weapon.configureMovement(SPRITE_SPEED, "east");
        weapon.addCollidableSprite(new EnemySprite());

        smiley.configureWeapon(weapon);
        smiley.configureWeapon(KeyEvent.VK_Z);

        gameEnv.add(smiley);

        return gameEnv;
    }

    public static GameEnvironment initGame2L1 () throws ClassNotFoundException {

        // initialize game environment
        GameEnvironment gameEnvL1 = initLevel2();
         gameEnvL1.setBackground("/resources/space.png");

        AllySprite bonus = new AllySprite("../resources/star.png");
        bonus.configureMovement(SPRITE_SPEED, "west");
        bonus.configureHP(1);
        bonus.addCollidableSprite(new ResponsiveSprite());

        EnemySprite fire = new EnemySprite("../resources/fire.png");
        fire.configureHP(1);
        fire.configureMovement(SPRITE_SPEED, "west");
        fire.addCollidableSprite(new EnemySprite());

        Random rand = new Random();

        for (int i = 1; i <= 10; i++) {
            int screenPosX = i * 600;
            int bonusNum = rand.nextInt(4);
            for (int j = 0; j < bonusNum; j++) {
                AllySprite bonusCopy = new AllySprite(bonus);
                int bonusPosX = screenPosX + rand.nextInt(600);
                int bonusPosY = rand.nextInt(700);
                bonusCopy.configurePosition(new Point(bonusPosX, bonusPosY));
                gameEnvL1.add(bonusCopy);
            }
            int fireNum = rand.nextInt(5);
            for (int j = 0; j < fireNum; j++) {
                EnemySprite fireCopy = new EnemySprite(fire);
                int firePosX = screenPosX + rand.nextInt(600);
                int firePosY = rand.nextInt(700);
                fireCopy.configurePosition(new Point(firePosX, firePosY));
                gameEnvL1.add(fireCopy);
            }
        }

        return gameEnvL1;
    }

    public static GameEnvironment initGame2L2 () throws ClassNotFoundException {

        // initialize game environment
        GameEnvironment gameEnvL2 = initLevel2();
        // gameEnvL1.setBackground("../resources/whiteBackground.jpg");

        EnemySprite jack = new EnemySprite("../resources/jack.png");
        jack.configureMovement(SPRITE_SPEED, "west");
        jack.addCollidableSprite(new ResponsiveSprite());
        jack.addCollidableSprite(new EnemySprite());

        EnemySprite fire = new EnemySprite("../resources/fire.png");
        fire.configureMovement(SPRITE_SPEED, "west");
        fire.addCollidableSprite(new EnemySprite());

        Random rand = new Random();

        for (int i = 1; i <= 10; i++) {
            int screenPosX = i * 600;
            int jackNum = rand.nextInt(4);
            for (int j = 0; j < jackNum; j++) {
                EnemySprite jackCopy = new EnemySprite(jack);
                int beanPosX = screenPosX + rand.nextInt(600);
                int beanPosY = rand.nextInt(700);
                jackCopy.configurePosition(new Point(beanPosX, beanPosY));
                gameEnvL2.add(jackCopy);
            }
            int fireNum = rand.nextInt(5);
            for (int j = 0; j < fireNum; j++) {
                EnemySprite fireCopy = new EnemySprite(fire);
                int firePosX = screenPosX + rand.nextInt(600);
                int firePosY = rand.nextInt(700);
                fireCopy.configurePosition(new Point(firePosX, firePosY));
                gameEnvL2.add(fireCopy);
            }
        }

        return gameEnvL2;
    }

    /**
     * @param args
     * @throws ClassNotFoundException
     */
    public static void main (String[] args) throws ClassNotFoundException {
        // TODO Auto-generated method stub
        GameController myGC = new GameController();
        myGC.add(new ViewController());
        myGC.add(new KeyboardListener());

        GameEnvironment gameLevel1 = initL1();
        myGC.add(gameLevel1);

        GameEnvironment gameLevel2 = initL2();
        myGC.add(gameLevel2);

        GameEnvironment gameLevel3 = initGame2L1();
        myGC.add(gameLevel3);

        GameEnvironment gameLevel4 = initGame2L2();
        myGC.add(gameLevel4);

        myGC.run();

    }

}
